package com.dudsbury.jtablestuff;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;

import javax.swing.DefaultListSelectionModel;
import javax.swing.JTable;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableModel;

import org.joda.time.DateTime;


import com.dudsbury.database.Booking;
import com.dudsbury.database.DatabaseMySQL;
import com.dudsbury.database.DatabaseExtension;
import com.dudsbury.database.MyDateTime;
import com.dudsbury.database.MyRoom;

import com.dudsbury.database.Booking;
import com.dudsbury.database.MyDateTime;
import com.dudsbury.gui.BookingFrame;
import com.dudsbury.gui.SearchScreen;

import com.dudsbury.other.Globals;




public class CustomTable extends JTable implements MouseListener,Runnable {

	private MyDateTime[] dateList;
	private MyDateTime currentDate;
	private MyRoom[] roomList;
	private DefaultTableModel tableModel;
	CustomTable thisTable = this;
	private ArrayList<Booking> bookingList;
	private Booking[][] rowData;
	boolean hold = false;
	public final int WEEKLY_VIEW = 0;
	 public final int MONTHLY_VIEW = 1;
	private int currentView = 0;
	private JTable table;
	boolean ok = Globals.ok;
	
	public CustomTable(ArrayList<Booking> bookingList, MyRoom[] roomList) {
		System.out.println("TABLE CONSTRUCTOR");
		Globals.customTable=this;
		
		this.bookingList = bookingList;
		this.roomList = roomList;
		table = this;
		setSize(400, 300);
		getTableHeader().setReorderingAllowed(false);
		getTableHeader().setResizingAllowed(false);
		dateList = getWeekOfDates(new DateTime());
		
		currentView = 0;
		currentDate = new MyDateTime(new DateTime());
		this.addMouseListener(this);
		setWeeklyView();
		//setRowData();
		TableCellRenderer renderer = new CustomTableCellRenderer();
		try {
			this.setDefaultRenderer(Class.forName("java.lang.Object"), renderer);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
			System.exit(0);
		}
		

		
	}

	public void setRowData() {
		System.out.println("SET ROW DATA ");
		rowData = new Booking[roomList.length][dateList.length];
		
		System.out.println("Date List Length: " + dateList.length);
		System.out.printf("Row Data Length: %x=d y=%d\n",rowData.length,rowData[0].length);
		bookingList.clear();
		bookingList = new ArrayList<Booking>();
		bookingList= DatabaseExtension.getBookings();
		MyRoom rooms[];
		for (int y = 0; y < dateList.length; y++) {
			
			
				for (Booking b : bookingList) {
					DateTime hold = dateList[y].getDateTime();
					if (b.getDate().getDayOfMonth()==hold.getDayOfMonth() && b.getDate().getMonthOfYear()==hold.getMonthOfYear() &&
							b.getDate().getYear()==hold.getYear()) {
						rooms = null;
						rooms = DatabaseExtension.getRoomsByBooking(b.getBookingId());
						
						for (int x = 0; x < roomList.length; x++) {
						for (MyRoom r: rooms) {
							//System.out.printf("R name: %s roomList name:%s\n", r.getRoomName(),roomList[x].getRoomName());
							if (r.getRoomName().equals(roomList[x].getRoomName())) {
								rowData[x][y] = b;
								System.out.println("Row Data = " + b);
							}
						}
						
					}
						
					}
				}

			}
		
		
		
		
				int rowIndex = table.getSelectedRow();
				int colIndex = table.getSelectedColumn();
				
			
			
		
		tableModel = new MyTableModel(rowData, dateList);
		this.setModel(tableModel);
		this.repaint();
		//System.out.printf("datelist: %d roomLIst: %d\n",rowData.length,rowData[0].length);
	}

	public void setWeeklyView() {
		System.out.println("WEEKLY VIEW");
		dateList = getWeekOfDates(currentDate.getDateTime());
		setRowData();
		tableModel = new MyTableModel(rowData, dateList);
		this.setModel(tableModel);
		currentView = WEEKLY_VIEW;
		
	}

	public void setMonthlyView() {
		System.out.println("MONTHLY VIEW");
		dateList = getMonthOfDates(currentDate.getDateTime());
		setRowData();
		
		tableModel = new DefaultTableModel(rowData, dateList);
		this.setModel(tableModel);
		repaint();
		currentView = MONTHLY_VIEW;
		
	}

	// Returns week of dates starting at the argument given
	public MyDateTime[] getWeekOfDates(DateTime d) {

		MyDateTime[] dateHold = new MyDateTime[7];
		//DateTime hold = new DateTime();
		for (int i = 0; i < 7; i++) {
			dateHold[i] = new MyDateTime(d.plusDays(i));

		}
		return dateHold;

	}

	// Returns month of dates starting at the argument given
	public MyDateTime[] getMonthOfDates(DateTime d) {

		//DateTime hold = new DateTime();
		MyDateTime[] dateHold = new MyDateTime[d.dayOfMonth()
				.getMaximumValue()];

		for (int i = 0; i < dateHold.length; i++) {
			dateHold[i] = new MyDateTime(d.plusDays(i));

		}
		return dateHold;

	}

	public int getCurrentView() {
		return currentView;
	}

	public void setCurrentView(int currentView) {
		this.currentView = currentView;
	}
	public MyDateTime getCurrentDate() {
		return currentDate;
	}

	public void setCurrentDate(MyDateTime currentDate) {
		this.currentDate = currentDate;
	}

	@Override
	public void mousePressed(MouseEvent arg0) {
		System.out.println("JTABLE MOUSE PRESSED");
		
		
		System.out.println("Number of Mouse listeners: "  +thisTable.getMouseListeners().length);
		new Thread(this).start();
		int rowIndex = getSelectedRow();
		int colIndex = getSelectedColumn();
		
		DateTime time = dateList[colIndex].getDateTime();
		
		//System.out.println(time);
		Booking thisBooking = rowData[rowIndex][colIndex];
		
		if (!Globals.ok) {
			System.out.println("Cannot open: " + Globals.ok);
			return;
		}
		if (thisBooking==null) {
			
			new BookingFrame(time,roomList[rowIndex]); 
			ok=false;
			new Thread(this).start();
			return;
				}
		else {
			//SearchScreen screen = new SearchScreen();
			//screen.setVisible(false);
			
			new BookingFrame(thisBooking,roomList[rowIndex],null);
			//DatabaseExtension.printBooking(thisBooking);
			ok=false;
			new Thread(this).start();
			return;
		}
	}

	@Override
	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		
		
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		
		
	
		
		
	}

	public void run() {
		
		System.out.println("New Thread Run");
		long time = System.currentTimeMillis();
		
		while (System.currentTimeMillis()<time+5000) {
			;
		}
		ok=true;
		
		System.out.println("Thread ended, ok= " + Globals.ok);
	}
	
	
}
